﻿using MediatR;
using SystemLYH.API.Application.Command.ReservoirCommand;
using SystemLYH.Domain;
using SystemLYH.Domain.Dto;
using SystemLYH.Infrstructre.Interface;

namespace SystemLYH.API.Application.Handlers.ReservoirHandlers
{
    public class ReservoirShowHandlers : IRequestHandler<ReservoirShowCommand, PageDTO<Reservoir>>
    {
        protected readonly IReservoirRepository _reservoirRepository;
        /// <summary>
        /// 库区构造函数
        /// </summary>
        /// <param name="reservoirRepository"></param>
        public ReservoirShowHandlers(IReservoirRepository reservoirRepository)
        {
            _reservoirRepository = reservoirRepository;
        }
        public async Task<PageDTO<Reservoir>> Handle(ReservoirShowCommand request, CancellationToken cancellationToken)
        {
            PageDTO<Reservoir> page =new PageDTO<Reservoir>();
            var list = (await _reservoirRepository.GetAllAsync()).Where(x => !x.IsDelReservoir);
            //库区名称
            if(request.ReservoirName != null)
            {
                list = list.Where(x => x.ReservoirName == request.ReservoirName);
            }
            //库区编号
            if(request.ReservoirNumber != null)
            {
                list = list.Where(x => x.ReservoirNumber == request.ReservoirNumber);
            }
            page.pageRoot = list.Count();
            page.pageCount = (int)Math.Ceiling(page.pageRoot * 1.0 / request.pageSize);
            page.plist=list.OrderByDescending(x=>x.ReservoirId).Skip((request.pageIndex-1)*request.pageSize).Take(request.pageSize).ToList();
            return page;
        }
    }
}
